﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using zevenseas.SharePoint.Solutions.FeatureBlocker.ApplicationPages;
using Microsoft.SharePoint.Utilities;

namespace zevenseas.SharePoint.Solutions.FeatureBlocker.ApplicationPages
{
    public class Configuration : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {        
        protected WebApplicationSelector uxWebApplicationSelector;
     
        protected InputFormRadioButton uxHide;
        protected InputFormRadioButton uxBlock;
        protected InputFormRadioButton uxRedirect;

        protected InputFormTextBox uxRedirectUrl;
        protected InputFormTextBox uxListUrl;

        protected InputFormCheckBox uxFarmAdmin;

        protected InputFormSection ifmRedirect;

        protected void uxRedirect_Changed(object sender, EventArgs e)
        {           
            if (uxRedirect.Checked)
            {
                ifmRedirect.Visible = true;
            }
            if (!uxRedirect.Checked)
            {
                ifmRedirect.Visible = false;
            }
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            if (uxWebApplicationSelector != null)
            {
                SPWebApplication webApplication = uxWebApplicationSelector.CurrentItem;
                FeatureBlockerConfig config = webApplication.GetChild<FeatureBlockerConfig>("FeatureBlockerConfig");
                if (config != null)
                {
                    switch (config.Action)
                    {
                        case TypeOfAction.Block:
                            uxHide.Checked = false;
                            uxBlock.Checked = true;
                            uxRedirect.Checked = false;
                            break;
                        case TypeOfAction.Hide:
                            uxHide.Checked = true;
                            uxBlock.Checked = false;
                            uxRedirect.Checked = false;
                            break;
                        case TypeOfAction.Redirect:
                            uxRedirect.Checked = true;
                            uxHide.Checked = false;
                            uxBlock.Checked = false;
                            uxRedirectUrl.Text = config.redirectUrl;
                            uxListUrl.Text = config.listUrl;
                            break;
                    }

                    config.ExcludeFarmAdmin = uxFarmAdmin.Checked;
                }
            }          

            base.OnLoadComplete(e);
        }


        public void OnClickOK(object sender, EventArgs e)
        {
            SPWebApplication webApplication = uxWebApplicationSelector.CurrentItem;
            FeatureBlockerConfig config = webApplication.GetChild<FeatureBlockerConfig>("FeatureBlockerConfig");
            if (config == null)
            {
                config = new FeatureBlockerConfig("FeatureBlockerConfig", webApplication, Guid.NewGuid());
                config.Update();
            }

            TypeOfAction typeOfAction = new TypeOfAction();

            if (uxHide.Checked)
            {
                typeOfAction = TypeOfAction.Hide;
            }
            if (uxBlock.Checked)
            {
                typeOfAction = TypeOfAction.Block;
            }
            if (uxRedirect.Checked)
            {
                typeOfAction = TypeOfAction.Redirect;
                config.redirectUrl = uxRedirectUrl.Text.ToString();
                config.listUrl = uxListUrl.Text.ToString();
            }

            config.ExcludeFarmAdmin = uxFarmAdmin.Checked;

            config.Action = typeOfAction;
            config.Update();
        }

        public void CreateList(object sender, EventArgs e)
        {
            SPUtility.Redirect("/_admin/zs.FeatureBlocker/createlistpage.aspx", SPRedirectFlags.Default, this.Context);
        }

        public static void SetWebProperty(string propertyValue, string propertyName, SPPropertyBag siteWebProperties)
        {
            string propertyname = "zs_" + propertyName;

            if (siteWebProperties[propertyname] == null)
            {
                siteWebProperties.Add(propertyname, propertyValue);
            }
            else
            {
                siteWebProperties[propertyname] = propertyValue;
            }
            siteWebProperties.Update();
        }

        public static string GetWebProperty(string propertyName, SPPropertyBag siteWebProperties)
        {
            string propertyname = "zs_" + propertyName;
            string returnvalue;

            if (siteWebProperties[propertyname] == null)
            {
                returnvalue = "";
            }
            else
            {
                returnvalue = siteWebProperties[propertyname];
            }

            return returnvalue;
        }
    }


}
